<?php

/**
 * Yii Controller HeaderController
 *
 * @author nguyenmanhluu
 * @since Aug 14, 2013 - 3:35:52 PM
 */
class FrontElementController extends MyController
{

    public $optsCate = 'Header';

    public function actionIndex()
    {
        $this->checkAccess();

        if (isset($_FILES['Options']))
        {
            $optsPhoto = $this->_uploadOptionsPhoto('opts', $_FILES['Options']);
            foreach ($optsPhoto as $id => $value)
            {
                $option = Options::model()->findByPk($id);
                if ($option != NULL)
                {
                    $option->value = $value;
                    $option->update(array('value'));
                }
            }
        }

        if (isset($_POST['Options']))
        {
            foreach ($_POST['Options'] as $id => $value)
            {
                $option = Options::model()->findByPk($id);
                if ($option != NULL)
                {
                    $option->value = $value;
                    $option->update(array('value'));
                }
            }
        }

        $opts = Options::model()->findAll($this->criteria());
        $this->render('//header/index', array('options' => $opts));
    }

    protected function criteria()
    {
        $crit            = new CDbCriteria();
        $crit->condition = 'category=:c';
        $crit->params    = array(':c' => Options::CAT_HEADER);

        return $crit;
    }

    protected function _uploadOptionsPhoto($dir, $file)
    {
        $iArray = array();
        if (@$file)
        {
            foreach ($file['name'] as $fkey => $fname)
            {
                // Lấy extension của file và tạo tên file mới
                $ext      = pathinfo($fname, PATHINFO_EXTENSION);
                $tmp_file = "files/$dir/" . md5(time() . $fname) . ".$ext";

                // Move file vào thư mục cần lưu                
                if (@move_uploaded_file($file['tmp_name'][$fkey], $tmp_file))
                {
                    $iArray[$fkey] = $tmp_file;
                }
            }
        }
        return $iArray;
    }

}

/** 
 * End file HeaderController
 */
